/**
 * output package name
 */
package com.kingdee.eas.wmsystem.zygl.bd.client;

import java.awt.event.ActionEvent;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.kdf.table.ICell;
import com.kingdee.bos.ctrl.kdf.table.event.KDTSelectEvent;
import com.kingdee.bos.ctrl.swing.KDPromptSelector;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.ui.face.IUIFactory;
import com.kingdee.bos.ui.face.IUIObject;
import com.kingdee.bos.ui.face.IUIWindow;
import com.kingdee.bos.ui.face.UIFactory;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.eas.common.client.UIFactoryName;
import com.kingdee.eas.util.client.KDTableUtil;
import com.kingdee.eas.wmsystem.zygl.bd.GoodsBomFactory;
import com.kingdee.eas.wmsystem.zygl.bd.GoodsInfo;
import com.kingdee.util.StringUtils;

/**
 * output class name
 */
public class GoodsBomSelectorUI extends AbstractGoodsBomSelectorUI implements KDPromptSelector {
	private static final Logger logger = CoreUIObject.getLogger(GoodsBomSelectorUI.class);

	boolean isCancel = true;

	private GoodsInfo goodInfo = null;

	private ICell cell = null;
	
	private String number = null;

	public GoodsBomSelectorUI(GoodsInfo goodsinfo, ICell cellParam) throws Exception {
		super();
		goodInfo = goodsinfo;
		cell = cellParam;
	}

	public GoodsBomSelectorUI() throws Exception {
		super();
	}

	public Object getData() {
		if (isCancel)
			return null;
		if (KDTableUtil.getSelectedRow(tblBom) != -1) {
			String id = (String) tblBom.getRow(KDTableUtil.getSelectedRow(tblBom)).getCell("id").getValue();
			try {
				return GoodsBomFactory.getRemoteInstance().getGoodsBomInfo(new ObjectUuidPK(id));
			} catch (Exception e) {
				handUIException(e);
			}
		}
		return null;

	}

	public boolean isCanceled() {

		return isCancel;
	}

	public void onLoad() throws Exception {

		super.onLoad();

		GoodsInfo gInfo = (GoodsInfo) getUIContext().get("goodInfo");
		cell = (ICell) getUIContext().get("cell");
		if (gInfo != null)
			goodsID = gInfo.getId().toString();
		
		number = (String) getUIContext().get("number");
		fillBomTable();

	}

	private void fillBomTable() {
		if (StringUtils.isEmpty(goodsID))
			this.queryBOMQuery = null;
		else {
			FilterInfo filter = new FilterInfo();
			filter.getFilterItems().add(new FilterItemInfo("gp.id", this.goodsID));
			filter.getFilterItems().add(new FilterItemInfo("id", null,CompareType.NOTEMPTY));
			if(!StringUtils.isEmpty(number))
			{
				filter.getFilterItems().add(new FilterItemInfo("bomNumber", "%"+number+"%",CompareType.LIKE));
			}
			queryBOMQuery = new EntityViewInfo();
			queryBOMQuery.setFilter(filter);
		}
		super.tblBom.removeRows();
		tblBom.setEnabled(false);
		if (tblBom.getRowCount() > 0) {
			tblBom.getSelectManager().select(0, 0);
		}

	}

	protected void tblBom_tableSelectChanged(KDTSelectEvent e) throws Exception {
		int selectedRow = KDTableUtil.getSelectedRow(tblBom);
		if (selectedRow == -1)
			return;

		String bomID = (String) tblBom.getRow(selectedRow).getCell("id").getValue();
		fillBomDetail(bomID);

	}

	private void fillBomDetail(String bomID) {

		if (StringUtils.isEmpty(bomID))
			this.queryBOMDetailQuery = null;
		else {
			FilterInfo filter = new FilterInfo();
			filter.getFilterItems().add(new FilterItemInfo("parentBom.id", bomID));
			queryBOMDetailQuery = new EntityViewInfo();
			queryBOMDetailQuery.setFilter(filter);
		}
		super.tblBomComp.removeRows();
		tblBomComp.setEnabled(false);

	}

	protected void btnCancel_actionPerformed(ActionEvent e) throws Exception {

		super.btnCancel_actionPerformed(e);
		isCancel = true;
		if (cell != null) {
			cell.setValue(getData());
		}
		getUIContext().clear();
		this.destroyWindow();
	}

	protected void btnComfirm_actionPerformed(ActionEvent e) throws Exception {

		super.btnComfirm_actionPerformed(e);
		isCancel = false;
		if (cell != null) {
			cell.setValue(getData());
		}
		getUIContext().clear();
		this.destroyWindow();
	}

	public void show() {
		IUIFactory uiFactory = null;
		try {
			UIContext uiContext = new UIContext(this);
			uiContext.put("goodInfo", goodInfo);
			uiContext.put("cell", cell);
			

			uiFactory = UIFactory.createUIFactory(UIFactoryName.MODEL);
			IUIWindow currOrgTreeDialog = uiFactory.create(GoodsBomSelectorUI.class.getName(), uiContext, null, "VIEW");

			currOrgTreeDialog.show();
		} catch (BOSException ex) {
			handUIException(ex);
		}
	}

	private String goodsID = null;

}